<!--
 * @Author: 千锋爱佛僧
 * @公众号: 大前端私房菜
 * @Slogan: 千锋精品教程，好学得不像实力派！
-->
<template>
  <el-row class="row-cont" justify="center" align="middle">
    <el-col :span="8" :offset="0">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <template #header>
          <div>
            <span>请登录123</span>
          </div>
        </template>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model.number="ruleForm.username" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="ruleForm.password"
              type="password"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">登录</el-button>
            <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-col>
  </el-row>
</template>
<script>
export default {
  data() {
    // 表单自定义验证函数
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入账号"));
      }
      if (value.length < 3) {
        return callback(new Error("账号长度至少3位"));
      }
      callback();
    };
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入密码"));
      }
      if (value.length < 3) {
        return callback(new Error("密码长度至少3位"));
      }
      callback();
    };
    return {
      ruleForm: {
        username: "大boss",
        password: "123",
      },
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePassword, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      let formEl = this.$refs.ruleFormRef;
      if (!formEl) return;
      formEl.validate((valid) => {
        if (valid) {
          this.$store.dispatch("user/userLoginAction", this.ruleForm);
        } else {
          console.log("error submit!");
          return false;
        }
      });
    },
  },
};
</script>
<style scoped>
.row-cont {
  height: 100vh;
}
</style>
